package com.scu.service;

import com.scu.pojo.UserSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.scu.pojo.User;
import com.scu.dao.UserDao;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@Service
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;


    /**
     * 获取所有的部门信息
     * @return
     */
    @Override
    public List<User> checkLoginInfo(String email,String pwd){
        return userDao.checkLoginInfo(email, pwd);
    }

    @Override
    public void addRegisterInfo(User user) {
        userDao.addRegisterInfo(user);
    }

    @Override
    public int modifyOnlineInfo(int id,int status) {
        return userDao.modifyOnlineInfo(id,status);
    }

    @Override
    public List<User> checkFriends(int id) {
        return userDao.checkFriends(id);
    }

    @Override
    public int addFriend(int source_id, int target_id) {
        return userDao.addFriend(source_id, target_id);
    }

    @Override
    public int deleteFriend(int source_id, int target_id) {
        return userDao.deleteFriend(source_id, target_id);
    }

    @Override
    public int modifyLoginInfo(User user) {
        return userDao.modifyLoginInfo(user);
    }

    @Override
    public List<User> checkFriendsByPage(UserSearch info) {
        return userDao.checkFriendsByPage(info,info.getPage()-1,info.getLimit());
    }

    @Override
    public int getFriendsCount(UserSearch info) {
        return userDao.getFriendsCount(info);
    }

    @Override
    public int checkIsFollowedByMe(int u_id, int target_id) {
        return userDao.checkIsFollowedByMe(u_id,target_id);
    }

    @Override
    public List<User> checkMyFollows(int u_id) {
        return userDao.checkMyFollows(u_id);
    }
    @Override
    public void approvalFile(MultipartFile filecontent, String realPath){
        OutputStream os = null;
        InputStream inputStream = null;
        String fileName = null;
        try {
            inputStream = filecontent.getInputStream();
            fileName = filecontent.getOriginalFilename();
            System.out.println("fileName="+fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String path =  realPath + "/static/img/userImg";
            // 2、保存到临时文件
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流保存到本地文件
            File tempFile = new File(path);
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
            // 开始读取
            while ((len = inputStream.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 完毕，关闭所有链接
            try {
                os.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

